package com.baichang.taoshunda.utils;

import com.baichang.framework.utils.DateUtil;

import java.util.*;

/**
 * Created by ${wushenglu} on 2018/3/5.
 */
public class ListGroupByTime {
    public static List<Map<String, Object>> groupByDateList(List<Map<String, Object>> mapList, String KeyValues) {
        List<Map<String, Object>> returnMaps = new ArrayList<Map<String, Object>>();
        if (mapList != null && mapList.size() > 0) {
            List<String> dates = new ArrayList<String>();
            for (Map<String, Object> map : mapList) {
                Date date = ConvertTypeUtils.obj2DateTime(map.get(KeyValues));
                String dateStr = DateUtil.formatDate(date, DateUtil.DATE_FORMAT);
                if (!dates.contains(dateStr)) {
                    dates.add(dateStr);
                }
            }
            if (dates != null && dates.size() > 0) {
                for (String str : dates) {
                    Map<String, Object> dateMap = new HashMap<String, Object>();
                    dateMap.put("date", str);
                    List<Map<String, Object>> maps = new ArrayList<>();
                    for (Map<String, Object> map : mapList) {
                        Date date = ConvertTypeUtils.obj2DateTime(map.get(KeyValues));
                        String dateStr = DateUtil.formatDate(date, DateUtil.DATE_FORMAT);
                        if (dateStr.equals(str)) {
                            maps.add(map);
                        }
                    }
                    dateMap.put("list", maps);
                    returnMaps.add(dateMap);
                }
            }
        }
        return returnMaps;
    }
}
